package com.alibaba.algo.apiimpl.feign;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.feign.service.OrgLabelService;
import com.alibaba.algo.cooperation.feign.vo.req.FeignBasePageReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectEmployeeListQeqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectOrgUserEmployeeVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserLabelReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.OrgLabelInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.OrgLabelUserInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserInfoResVO;
import com.alibaba.algo.core.base.Pager;
import com.alibaba.algo.domain.user.SysLabelDomain;
import com.alibaba.algo.domain.user.SysOrgDomain;
import com.alibaba.algo.domain.user.SysRoleDomain;
import com.alibaba.algo.domain.user.SysUserDomain;
import com.alibaba.algo.dtos.user.SysOrgDTO;
import com.alibaba.algo.entity.user.SysLabel;
import com.alibaba.algo.util.StringUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位标签外部接口实现
 *
 * @author Songtinghua
 * @create 2021-11-03 13:47
 **/
@RestController
@Slf4j
public class OrgLabelServiceImpl implements OrgLabelService {

    @Autowired
    SysLabelDomain sysLabelDomain;
    @Autowired
    private SysUserDomain sysUserDomain;
    @Autowired
    private SysRoleDomain sysRoleDomain;
    @Autowired
    private SysOrgDomain sysOrgDomain;

    @Override
    public PagerEntity<OrgLabelInfoResVO> selectList(@RequestBody FeignBasePageReqVO param) {
        if (null == param) {
            param = new FeignBasePageReqVO();
            param.setPageIndex(1);
            param.setPageIndex(10);
        }
        if (null == param.getPageIndex() || param.getPageIndex() < 0) {
            param.setPageIndex(1);
        }
        if (null == param.getPageSize() || param.getPageSize() <= 0) {
            param.setPageSize(10);
        }
        List<OrgLabelInfoResVO> labels = Lists.newArrayList();
        List<SysLabel> sysLabels = sysLabelDomain.selectAllLabel(param);
        PagerEntity<OrgLabelInfoResVO> pagerEntity;
        if (!CollectionUtils.isEmpty(sysLabels)) {
            sysLabels.forEach(a -> {
                OrgLabelInfoResVO resVO = new OrgLabelInfoResVO();
                BeanUtils.copyProperties(a, resVO);
                labels.add(resVO);
            });
            Integer count = sysLabelDomain.selectAllLabelCount();
            Pager pager = Pager.newInstance(Long.parseLong(count.toString()), param.getPageIndex(), param.getPageSize());
            pagerEntity = new PagerEntity<>(count, pager.getTotalPages(), param.getPageIndex(), param.getPageSize(), labels);
        } else {
            pagerEntity = new PagerEntity<>(0, 0, param.getPageIndex(), param.getPageSize(), labels);
        }
        return pagerEntity;
    }

    @Override
    public PagerEntity<OrgLabelUserInfoResVO> selectEmployeeByOrgLabel(@RequestBody SelectEmployeeListQeqVO param) {
        PagerEntity<OrgLabelUserInfoResVO> pagerEntity = new PagerEntity<>(0, 0, 1, 10, null);
        if (null == param || StringUtils.isEmpty(param.getLabelCode())) {
            return pagerEntity;
        }
        if (null == param.getPageIndex() || param.getPageIndex() < 0) {
            param.setPageIndex(1);
        }
        if (null == param.getPageSize() || param.getPageSize() <= 0) {
            param.setPageSize(10);
        }
        List<OrgLabelUserInfoResVO> orgLabelUserInfos = sysLabelDomain.selectUserByOrgLabel(param);
        if (!CollectionUtils.isEmpty(orgLabelUserInfos) && null != orgLabelUserInfos.get(0)) {
            Integer count = sysLabelDomain.selectUserCountByOrgLabel(param);
            Pager pager = Pager.newInstance(Long.parseLong(count.toString()), param.getPageIndex(), param.getPageSize());
            pagerEntity = new PagerEntity<>(count, pager.getTotalPages(), param.getPageIndex(), param.getPageSize(), orgLabelUserInfos);
        } else {
            pagerEntity = new PagerEntity<>(0, 0, param.getPageIndex(), param.getPageSize(), null);
        }
        return pagerEntity;
    }

    @Override
    public BaseResult<List<UserInfoResVO>> qryUserListByUserAndLabel(List<SelectUserLabelReqVO> selectUserLabelReqVOList) {
        // 校验参数
        selectUserLabelReqVOList = filterSelectUserLableReqVOList(selectUserLabelReqVOList);
        List<UserInfoResVO> userInfoResVOList = new ArrayList<>();
        selectUserLabelReqVOList.stream().forEach(
                vo -> {
//                    // 查询标签及组织下的用户
//                    List<UserInfoResVO> userInfoResVOByLabel = sysLabelDomain.getUsersByLable(vo);
//                    userInfoResVOList.addAll(userInfoResVOByLabel);
                    SelectOrgUserEmployeeVO selectOrgUserEmployeeVO = new SelectOrgUserEmployeeVO();
                    BeanUtils.copyProperties(vo,selectOrgUserEmployeeVO);
                    List<SysOrgDTO> sysOrgList = sysOrgDomain.selectOrgByEmployeeCondition(selectOrgUserEmployeeVO);
                    if(!ObjectUtils.isEmpty(sysOrgList)){
                        sysOrgList.stream().forEach(
                                sysOrg -> {
                                    SelectEmployeeListQeqVO param = new SelectEmployeeListQeqVO();
                                    param.setLabelCode(vo.getLabelCode());
                                    param.setOrgId(sysOrg.getId());
                                    param.setPageIndex(1);
                                    param.setPageSize(20);
                                    List<OrgLabelUserInfoResVO> orgLabelUserInfoList = sysLabelDomain.selectUserByOrgLabel(param);
                                    if(!ObjectUtils.isEmpty(orgLabelUserInfoList)){
                                        orgLabelUserInfoList.stream().forEach(
                                                orgLabelUserInfo -> {
                                                    UserInfoResVO userInfoResVO = new UserInfoResVO();
                                                    BeanUtils.copyProperties(orgLabelUserInfo,userInfoResVO);
                                                    userInfoResVOList.add(userInfoResVO);
                                                }
                                        );
                                    }
                                }
                        );
                    }
                }
        );
        return BaseResult.buildSuccess(userInfoResVOList);
    }

    private List<SelectUserLabelReqVO> filterSelectUserLableReqVOList(List<SelectUserLabelReqVO> selectUserLabelReqVOList) {
        selectUserLabelReqVOList = selectUserLabelReqVOList.stream().filter(
                vo ->{
                    if(ObjectUtils.isEmpty(vo.getUserId())
                            && ObjectUtils.isEmpty(vo.getUserAccount())
                            && ObjectUtils.isEmpty(vo.getEmployeeCode())){
                        log.warn("User is null.Params are vo:{}",vo);
                        return false;
                    }
                    if(ObjectUtils.isEmpty(vo.getLabelId())
                            && ObjectUtils.isEmpty(vo.getLabelCode())){
                        log.warn("Label is null.Params are vo:{}",vo);
                        return false;
                    }
                    if(!StringUtils.isEmpty(vo.getUserAccount()) && !StringUtil.isAlnum(vo.getUserAccount())){
                        log.warn("UserAccount contains special characters.Params are vo:{}",vo);
                        return false;
                    }
                    return true;
                }
        ).collect(Collectors.toList());
        return selectUserLabelReqVOList;
    }
}
